<?php

    
    include("../bd/conexao/config.php"); // Arquivo de configuração MySQL   
 

    $sql     = "SELECT * FROM invoices";
    $query     = mysql_query($sql);       
    $resultado = mysql_num_rows($query); //traz o resultado da pesquisa acima

    while($row = mysql_fetch_array($query)) {

        $result[$row['area_nome']][]        = $row['nome_funcionario'];    
        $ramal[$row['nome_funcionario']][]  = $row['ramal'];
        $ultima_atualizacao = $row['timestamp'];
        
    }
     echo $ultima_atualizacao;
     print_r($result);
     echo "<br>";
     print_r($ramal);
     echo "<br>";
?>

<!DOCTYPE html>
<html>

    <head>
        <!-- Metas -->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="author" content="Truth" />

        <title>Invoice Rowspan Example</title>

    </head>

    <body>

        <?php

           /*$array = array(0 => array(0 => 1, 1 => 1), 1 => array(0 => 1, 1 => 2), 2 => array(0 => 1, 1 => 5), 3 => array(0 => 2, 1 => 3), 4 => array(0 => 3, 1 => 4));       
                     
               for($i = 0; $i < count($array); $i++) {

                  for($j = 0; $j <= 1; $j++) {

                    printf("array[%d][%d]: %d\n", $i, $j, $array[$i][$j]);
                    echo "<br>";
                    //echo $array[$i][$j] . " ";

                  }
                  echo "<br>";
               }

                echo "tamanho array: " .count($array) . "<br><br>"; */
           
        ?>
       
        <table id="invoices" border="1" style="width:50%;">
            <thead>
                <th>Area</th>
                <th>Funcionario</th>
                <th>Ramal</th>
            </thead>
            <tbody>
                <?php
                    
                    foreach($result as $nome_ar => $areas) {
                        echo count($areas). " ";
                        echo '<tr>';
                        echo '<td rowspan='. count($areas) . '>' . $nome_ar . '</td>';
                        $count = 0;
                        
                        foreach ($areas as $area) {
                            if ($count != 0) {
                                echo '<tr>';
                            }
                            echo "<td>$area</td>"; 
                            echo "<td>{$ramal[$area][0]}</td>"; 
                            echo "</tr>";
                            $count++;                           
                           
                        }
                        
                    }

                    echo "<br> Ultima atualizacao: " . $ultima_atualizacao;

                ?>
            </tbody>
        </table>

    </body>
</html>